﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace AndersLiu.EmailQueue.Service
{
	/// <summary>
	/// 用于读取配置文件中的EmailQueueService节。
	/// </summary>
	public class EmailQueueServiceConfigSection : ConfigurationSection
	{
		/// <summary>
		/// 获取配置文件中的EmailQueue节。
		/// </summary>
		/// <returns></returns>
		public static EmailQueueServiceConfigSection GetConfig()
		{
			return (EmailQueueServiceConfigSection)ConfigurationManager.GetSection("EmailQueueService");
		}
	}

	/// <summary>
	/// 表示EmailQueueService/Threads配置集合。
	/// </summary>
	[ConfigurationCollection(typeof(ThreadConfigElement), AddItemName = "Thread", CollectionType = ConfigurationElementCollectionType.BasicMap)]
	public class ThreadConfigCollection : ConfigurationElementCollection
	{
		protected override ConfigurationElement CreateNewElement()
		{
			return new ThreadConfigElement();
		}

		protected override object GetElementKey(ConfigurationElement element)
		{
			return ((ThreadConfigElement)element).Name;
		}
	}


	/// <summary>
	/// 表示EmailQueueService/Threads/Thread配置。
	/// </summary>
	public class ThreadConfigElement : ConfigurationElement
	{
		/// <summary>
		/// 获取EmailQueueService/Threads/Thread/Name配置。
		/// 表示该线程的名字。该名字必须唯一，用于标识一个线程。
		/// </summary>
		[ConfigurationProperty("Name", IsRequired = true, IsKey = true)]
		public string Name
		{
			get { return (string)base["Name"]; }
		}

		/// <summary>
		/// 获取EmailQueueService/Threads/Thread/ApplicationName配置。
		/// 应用程序的名字。该线程只从队列中取具有该名字的消息进行发送。
		/// </summary>
		[ConfigurationProperty("ApplicationName", IsRequired = true)]
		public string ApplicationName
		{
			get { return (string)base["ApplicationName"]; }
		}

		/// <summary>
		/// 获取EmailQueueService/Threads/Thread/SendingInterval配置。
		/// 每发送两封邮件之间的间隔时间。
		/// </summary>
		[ConfigurationProperty("SendingInterval", IsRequired = true)]
		public TimeSpan SendingInterval
		{
			get { return (TimeSpan)base["SendingInterval"]; }
		}

		/// <summary>
		/// 获取EmailQueueService/Threads/Thread/PollingInterval配置。
		/// 当队列中没有邮件时，多长时间检查一次队列。
		/// </summary>
		[ConfigurationProperty("PollingInterval", IsRequired = true)]
		public TimeSpan PollingInterval
		{
			get { return (TimeSpan)base["PollingInterval"]; }
		}

		/// <summary>
		/// 获取EmailQueueService/Threads/Thread/Host配置。
		/// SMTP服务器地址。
		/// </summary>
		[ConfigurationProperty("Host", IsRequired = true)]
		public string Host
		{
			get { return (string)base["Host"]; }
		}

		/// <summary>
		/// 获取EmailQueueService/Threads/Thread/Port配置。
		/// SMTP服务器端口。
		/// </summary>
		[ConfigurationProperty("Port", IsRequired = true)]
		public int Port
		{
			get { return (int)base["Port"]; }
		}

		/// <summary>
		/// 获取EmailQueueService/Threads/Thread/EnableSsl配置。
		/// 服务器是否要求启用SSL。
		/// </summary>
		[ConfigurationProperty("EnableSsl", IsRequired = true)]
		public bool EnableSsl
		{
			get { return (bool)base["EnableSsl"]; }
		}

		/// <summary>
		/// 获取EmailQueueService/Threads/Thread/UserName配置。
		/// 用户名。
		/// </summary>
		[ConfigurationProperty("UserName", IsRequired = true)]
		public string UserName
		{
			get { return (string)base["UserName"]; }
		}

		/// <summary>
		/// 获取EmailQueueService/Threads/Thread/Password配置。
		/// 密码。
		/// </summary>
		[ConfigurationProperty("Password", IsRequired = true)]
		public string Password
		{
			get { return (string)base["Password"]; }
		}

		/// <summary>
		/// 获取EmailQueueService/Threads/Thread/Address配置。
		/// 发送端Email地址。
		/// </summary>
		[ConfigurationProperty("Address", IsRequired = true)]
		public string Address
		{
			get { return (string)base["Address"]; }
		}

		/// <summary>
		/// 获取EmailQueueService/Threads/Thread/DisplayName配置。
		/// 显示的发送者名称。
		/// </summary>
		[ConfigurationProperty("DisplayName", IsRequired = true)]
		public string DisplayName
		{
			get { return (string)base["DisplayName"]; }
		}

		/// <summary>
		/// 获取EmailQueueService/Threads/Thread/Encoding配置。
		/// 邮件编码。
		/// </summary>
		[ConfigurationProperty("Encoding", IsRequired = true)]
		public Encoding Encoding
		{
			get { return (Encoding)base["Encoding"]; }
		}
	}
}
